package com.demo2do.boyle.backend.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.system.PrincipalService;
import com.demo2do.boyle.common.entity.system.Principal;
import com.demo2do.core.BusinessException;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

/**
 * 
 * @author leo
 *
 */
@Controller
@RequestMapping("/principals")
@MenuSetting("menu-system")
public class PrincipalController {
    
	@Autowired
	private PrincipalService principalService;
	
    /**
     * List all the principals
     * 
     * @return 
     */
    @RequestMapping("")
    @MenuSetting("submenu-system-principal-list")
    public ModelAndView list() {
    	
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-list");
        
        List<Principal> principals = principalService.list();
        modelAndView.addObject("principals", principals);
        
        return modelAndView;
    }
    
    /**
     * display the create panel
     * 
     * @return
     */
    @RequestMapping("/create")
    public String create() {
        return "system/principal/principal-create";
    }
    
    /**
     * do create
     * 
     * @param principal
     * @return the result
     */
    @RequestMapping( value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Principal principal) {
    	
    	Result result = new Result();
    	
    	try {
    		
    		principalService.create(principal.initialize());
    		
    		result.success().message("创建用户成功！");
    		
    	} catch(BusinessException e) {
    		
    		result.fail().message(e.getMessage());
    		
    	} catch(Exception e) {
    		
    		result.fail().message("服务器异常，请稍后再试！");
    		
    	}
    	
    	return JsonUtils.toJsonString(result);    	
    
    }
    
    /**
     * display the edit panel
     * 
     * @param id
     * @return the principal info edit view
     */
    @RequestMapping("/{id}/edit")
    public ModelAndView edit(@PathVariable("id") Integer id) {
    	
        ModelAndView modelAndView = new ModelAndView("system/principal/principal-edit");
        
        Principal principal = principalService.load(Principal.class, id);
        
        modelAndView.addObject("principal", principal);
        
        return modelAndView;
    }
    
    /**
     * do edit 
     * 
     * @param principal
     * @return
     */
    @RequestMapping(value="/{id}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading(value="id", type = Integer.class) Principal principal) {

        Result result = new Result();

        try {

        	principalService.update(principal);
            result.success().message("账号更新成功！");

        } catch(Exception e) {
            result.fail().message("服务器异常！");
        }

        return JsonUtils.toJsonString(result);	
    }   
    
    /**
     * display the reset-password panel
     * 
     * @return
     */
    @RequestMapping("/{id}/reset-password")
    public ModelAndView resetPassword(@PathVariable("id") Integer id) {
    	ModelAndView modelAndView = new ModelAndView("system/principal/principal-reset");
    	modelAndView.addObject("id", id);
        return modelAndView;
    }
    
    /**
     * do reset 
     * @param id
     * @return the result
     */
    @RequestMapping(value="/{id}/reset-password", method = RequestMethod.PATCH)
    public @ResponseBody String onResetPassword(@PathVariable("id") Integer id) {

        Result result = new Result();
        
        try {
        	
        	Principal principal = principalService.load(Principal.class, id);
        	
        	principalService.resetPassword(principal);

        	result.success().message("重置密码成功");
        	
        } catch(Exception e) {
        	
            result.fail().message("服务器异常");
        
        }

        return JsonUtils.toJsonString(result);	
    }   
	
}
